package com.diyunapp.xilinapp.details;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.adapters.RecycleViewStoreListtAdapter;
import com.diyunapp.xilinapp.bean.StoreListBean;
import com.diyunapp.xilinapp.bean.YouLikeGoodsBean;
import com.diyunapp.xilinapp.main.MainActivity;
import com.diyunapp.xilinapp.myapi.OnRecyclerViewItemClickListener;
import com.diyunapp.xilinapp.selfdefine.StatusBarActivity;
import com.diyunapp.xilinapp.utils.FormatConfirm;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by NIDAYE on 2016/8/5.
 */
public class StoreListActivity extends StatusBarActivity {

    @Bind(R.id.storeresult_goback_ib)
    ImageButton storeresultGobackIb;
    @Bind(R.id.type_search_tv)
    TextView typeSearchTv;
    @Bind(R.id.store_key_ll)
    LinearLayout storeKeyLl;
    @Bind(R.id.search_storeslist_rv)
    RecyclerView searchStoreslistRv;

    private View head_status_view;

    private String searchKey;

    private LinearLayoutManager theManagerLL;
    private List<StoreListBean>  theStoresList;
    private RecycleViewStoreListtAdapter theStoreAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stores_searchresult_layout);
        ButterKnife.bind(this);

        searchKey = getIntent().getStringExtra("StoreKey");
        initViews();
        getStoreList();
    }

    private void initViews() {
        head_status_view = findViewById(R.id.storeresult_menubar_inc);
        super.MenuWidget(head_status_view);
        storeresultGobackIb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        typeSearchTv.setText(searchKey);

        theManagerLL = new LinearLayoutManager(this);
        theStoresList = new ArrayList<>();
        theStoreAdapter = new RecycleViewStoreListtAdapter(this,theStoresList);
        searchStoreslistRv.setLayoutManager(theManagerLL);
        searchStoreslistRv.setAdapter(theStoreAdapter);
        theStoreAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int data) {
                Intent storeInt = new Intent(StoreListActivity.this, StoreActivity.class);
                storeInt.putExtra("StoreId", theStoresList.get(data).getStoreId());
                startActivity(storeInt);
            }
        });
    }

    private void getStoreList() {
        String url = "http://www.xilingou.com/mobile/index.php?act=goods&op=sousuo&store_name=" + searchKey;
        Log.i("zhangxiaoxidebug","storelisturl="+url);
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(2000);
        http.send(HttpRequest.HttpMethod.GET, url, new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        int result = parseProject(responseInfo.result);
                        switch (result) {
                            case 1:
                                break;
                            case 0:
                                Toast.makeText(getApplicationContext(), "搜索店铺失败！", Toast.LENGTH_LONG).show();
                                break;
                            default:
                                break;
                        }
                    }


                    @Override
                    public void onStart() {

                    }

                    private int parseProject(String result) {
                        Log.i("zhangxiaoxidebug", "store_result=" + result);
                        try {
                            JSONObject jsObjectCode = new JSONObject(result);
                            int code = jsObjectCode.getInt("code");
                            if(code ==0)
                            {
                                typeSearchTv.setText("没有包含关键字 \""+searchKey + "\" 的店铺");
                                return 1;
                            }

                            if (code != 200) {
                                return 0;
                            }

                            theStoresList.clear();
                            JSONArray jsArrayData = jsObjectCode.getJSONArray("data");
                            for(int i=0;i<jsArrayData.length();i++)
                            {
                                JSONObject jsObjectStoreInfo = jsArrayData.getJSONObject(i);
                                String store_id = jsObjectStoreInfo.getString("store_id");
                                String store_name = jsObjectStoreInfo.getString("store_name");
                                String image = jsObjectStoreInfo.getString("image");
                                int credit_num = jsObjectStoreInfo.getInt("store_credit_average");
                                String xiaoliang = jsObjectStoreInfo.getString("xiaoliang");
                                String baobei = jsObjectStoreInfo.getString("baobei");
                                theStoresList.add(new StoreListBean(store_id,store_name,image,credit_num,xiaoliang,baobei));
                            }
                            theStoreAdapter.notifyDataSetChanged();
                        } catch (JSONException e) {
                            e.printStackTrace();
                            return 0;
                        }
                        return 1;
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        Log.i("zhangxiaoxidebug", "getIndex....onFailure msg=" + msg);
                        Toast.makeText(getApplicationContext(), "搜索店铺失败，请检查网络设置！", Toast.LENGTH_LONG).show();
                    }
                }
        );
    }
}
